﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace ryan
{

    class Circle
    {
        private Texture2D textureCircle;
        private GraphicsDevice graphicsDevice;
        private int radius;
        private bool fillCircle;
        private Color[] colorData;
        private Color colorIn, colorOut;

        public Circle(GraphicsDevice newGraphicsDevice, int newRadius, Color newColor)
        {
            this.graphicsDevice = newGraphicsDevice;
            SetRadius(newRadius);
            this.fillCircle = false;
            this.colorIn = newColor;
            this.colorOut = newColor;
        }

        public Texture2D GetCircle()
        {
            Refresh();
            return this.textureCircle;
        }

        public void SetRadius(int newRadius)
        {
            this.radius = newRadius;
        }

        public void SetFill(bool newFill)
        {
            this.fillCircle = newFill;
        }

        public void DrawFillCircle()
        {
            bool finished = false;
            int firstSkip = 0;
            int lastSkip = 0;

            for (int i = 0; i <= colorData.Length - 1; i++)
            {
                int outerRadius = radius * 2 + 2;
                if (finished == false)
                {
                    //T = transparent W = White;
                    //Find the First Batch of Colors TTTTWWWTTTT The top of the circle
                    if ((colorData[i] == this.colorOut) && (firstSkip == 0))
                    {
                        while (colorData[i + 1] == this.colorOut)
                        {
                            i++;
                        }
                        firstSkip = 1;
                        i++;
                    }
                    //Now Start Filling                       TTTTTTTTWWTTTTTTTT
                    //circle in Between                       TTTTTTW--->WTTTTTT
                    //transaparent blancks                    TTTTTWW--->WWTTTTT
                    //                                        TTTTTTW--->WTTTTTT
                    //                                        TTTTTTTTWWTTTTTTTT
                    if (firstSkip == 1)
                    {
                        if (colorData[i] == this.colorOut && colorData[i + 1] != this.colorOut)
                        {
                            i++;
                            while (colorData[i] != this.colorOut)
                            {
                                //Loop to check if its the last row of pixels
                                //We need to check this because of the 
                                //int outerRadius = radius * 2 + -->'2'<--;
                                for (int j = 1; j <= outerRadius; j++)
                                {
                                    if (colorData[i + j] != this.colorOut)
                                    {
                                        lastSkip++;
                                    }
                                }
                                //If its the last line of pixels, end drawing
                                if (lastSkip == outerRadius)
                                {
                                    break;
                                }
                                else
                                {
                                    colorData[i] = this.colorIn;
                                    i++;
                                    lastSkip = 0;
                                }
                            }
                            while (colorData[i] == this.colorOut)
                            {
                                i++;
                            }
                            i--;
                        }
                    }
                }
            }
        }

        public void Refresh()
        {
            int outerRadius = radius * 2 + 2; // So circle doesn't go out of bounds
            textureCircle = new Texture2D(graphicsDevice, outerRadius, outerRadius);
            colorData = new Color[outerRadius * outerRadius];

            // Colour the entire texture transparent first.
            for (int i = 0; i < colorData.Length; i++)
            {
                colorData[i] = Color.Transparent;
            }

            // Work out the minimum step necessary using trigonometry + sine approximation.
            double angleStep = 1f / radius;

            for (double angle = 0; angle < Math.PI * 2; angle += angleStep)
            {
                // Use the parametric definition of a circle: http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates
                int x = (int)Math.Round(radius + radius * Math.Cos(angle));
                int y = (int)Math.Round(radius + radius * Math.Sin(angle));

                colorData[y * outerRadius + x + 1] = this.colorOut;
            }

            if (this.fillCircle == true)
            {
                DrawFillCircle();
            }

            // Set the data when finished 
            //-- don't need to paste this part, already given up above
            textureCircle.SetData(colorData);
        }
    }
}
